<template>
  <div class="app-container">
    新增商品
    <div class="addP">
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm"
          v-loading="loading"
          style="width: 100%">
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="商品简介" prop="descriptions">
          <el-input v-model="ruleForm.descriptions" />
        </el-form-item>
        <el-form-item label="商品数量" prop="quantity">
          <el-input v-model="ruleForm.quantity" />
        </el-form-item>
        <el-form-item label="商品内容" prop="content">
          <el-input v-model="ruleForm.content" />
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input v-model="ruleForm.price" />
        </el-form-item>
        <el-form-item label="商品主图">
          <el-upload
            class="avatar-uploader"
            action="http://lanlianhua.work:8888/api/v1/common/file_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="ruleForm.imageUrl" :src="ruleForm.imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="商品分类ID" prop="productCategory">
          <el-input v-model="ruleForm.productCategory" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleForm);" :plain="true">立即创建</el-button>
             
          <!-- <el-button @click="resetForm(ruleForm)">重置</el-button> -->
           <el-popover
           placement="top"
           width="160"
           v-model="visible">
          <p>确定要重置添加操作吗？</p>
           <div style="text-align: right; margin: 0">
           <el-button size="mini" type="text" @click="visible = false">取消</el-button>
           <el-button type="primary" size="mini"  @click="resetForm(ruleForm);visible = false" >确定</el-button>
            </div>
           <el-button slot="reference" >重置</el-button>
         </el-popover>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { addProduct } from '@/api/product'
export default {
  components: {},
  data() {
    return {
      loading:false,
      visible: false,
      ruleForm: {
        name: '',
        descriptions: '',
        quantity: '',
        content: '',
        price: '',
        delivery: false,
        productCategory: '',
        imageUrl: '',
        coverImg: '',
        visible: false,
      },
      rules: {
        name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        descriptions: [
          { required: true, message: '请输入商品简介', trigger: 'blur' }
        ],
        quantity: [
          { required: true, message: '请输入商品数量', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入商品内容', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
        ],
        resource: [
          { required: true, message: '请选择活动资源', trigger: 'change' }
        ],
        productCategory: [
          { required: true, message: '请输入商品分类ID', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {

  },
  mounted() {

  },
  methods: {
    handleAvatarSuccess(res, file) {
      console.log(res)
      console.log(file)
      this.ruleForm.imageUrl = 'http://lanlianhua.work:8888' + res.info
    },
    resetForm() {
      this.ruleForm = {}
    },
    async submitForm(ruleForm) {
      this.loading=true
      const result = await addProduct({
        name: ruleForm.name,
        descriptions: ruleForm.descriptions,
        quantity: ruleForm.quantity,
        price: ruleForm.price,
        coverImg: ruleForm.imageUrl,
        productCategory: ruleForm.productCategory
      }).then((result)=>{
         this.loading=-false,
         console.log(66666, result)
       if(result._id!==""){
           this.$message({
          message: '添加商品成功',
         
          type: 'success',
          center:true,
          offset:200,
        });
        this.resetForm();
        }
      })
     
       
       /*  setTimeout(()=>{
          this.resetForm();
        },500);
        */
      },
     
    }
  }
</script>
<style scoped>
.addP{
  margin: 50px auto;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .addP button{
    margin-right: 20px;
  }
</style>
